/*******************************************************************************
 * Copyright (c) 2008 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package fr.jade.fraclite.orb;

import java.util.HashMap;
import java.util.Map;

import org.objectweb.fractal.api.Interface;
import org.objectweb.fractal.api.type.InterfaceType;

import fr.jade.fraclite.orb.api.Binder;
import fr.jade.fraclite.orb.api.Marshaller;
import fr.jade.fraclite.orb.api.Name;
import fr.jade.fraclite.orb.io.RmiMarshaller;
import fr.jade.fraclite.orb.names.JGroupsName;
import fr.jade.fraclite.orb.protocols.MulticastRpcServer;

/**
 * 
 * @author Sylvain Sicard
 *
 */
public class MulticastSkeleton implements Skeleton {
  //private JChannelFactory factorySkeleton = null;
  //Channel channel = null;
  Marshaller m;
	Map<Long, Object> targets = null;
	Binder binder;
	String groupId;
	
	public MulticastSkeleton(Binder binder){
	  this.binder = binder;
		this.targets = new HashMap<Long, Object>();;
		//this.factorySkeleton = new JChannelFactory();
		m = new RmiMarshaller(binder);
//    try {
//      factorySkeleton.setMultiplexerConfig("stacks.xml");
//    } catch (Exception e) {
//      e.printStackTrace();
//    }
	}

	@Override
  public void registerTarget(Object target) {
	  Long itfId = ((Interface)target).getFcItfGUId();
	  groupId = itfId.toString();
	  //System.out.println(((Interface)target).getFcItfName()+"::"+groupId);
	  try {
      //channel = factorySkeleton.createMultiplexerChannel("udp", groupId);
	    //channel = factorySkeleton.createChannel("udp");
      //HARpcDispatcher disp = new HARpcDispatcher(channel, null, null, target);
	    @SuppressWarnings("unused")
      MulticastRpcServer disp = new MulticastRpcServer(groupId, m, target);
//      disp.setMarshaller(m);
//      disp.setResponseMarshaller(m);
//      disp.setRequestMarshaller(m);

      //System.out.println("[skel] connect "+groupId);
      //channel.connect(groupId);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
	
	@Override
  public Name getName(Object target) {
	  long itfId = ((Interface)target).getFcItfGUId();
    String signature = ((InterfaceType)((Interface)target).getFcItfType()).getFcItfSignature();
    return new JGroupsName(itfId, signature, binder, groupId);
  }
	
	@Override
	public void run() {

	}


  

}
